package com.captain.special.二叉树.中等;

import com.captain.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/5/15 2:26
 */
public class 不同的二叉搜索树II95 {
    public static void main(String[] args) {
        new 不同的二叉搜索树II95().generateTrees(2);
    }

    public List<TreeNode> generateTrees(int n) {
        if (n < 1) {
            return new ArrayList<>();
        }
        return recur(1, n);
    }

    public List<TreeNode> recur(int start, int end) {
        List<TreeNode> ans = new ArrayList<>();
        //节点为空
        if (start > end) {
            ans.add(null);
            return ans;
        }
        for (int i = start; i <= end; i++) {
            //左节点
            List<TreeNode> left = recur(start, i - 1);
            List<TreeNode> right = recur(i + 1, end);
            for (TreeNode l : left) {
                for (TreeNode r : right) {
                    TreeNode node = new TreeNode(i);
                    node.left = l;
                    node.right = r;
                    ans.add(node);
                }
            }
        }
        return ans;
    }
}
